﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KraveEntities;
using System.Data.SqlClient;

namespace KraveDAL
{
    public class LoginDAL:DataBase
    {
        public bool save_login(LoginBO lg)
        {

            cmd.CommandText = "insert into login values('" + lg.Id + "','" + lg.Password + "' ,'" + lg.Type + "' )";
            int rows_affected = cmd.ExecuteNonQuery();
            if (rows_affected > 0)
            {
                return true;
            }
            else
                return false;
        }

        public int Check_login(LoginBO login, ref int id)
        {
            
            cmd.CommandText = "SELECT * from login ";

            SqlDataReader dr = cmd.ExecuteReader();

            while (dr.Read())
            {

                if (dr.GetInt32(0).Equals(login.Id) && dr.GetString(1).Equals(login.Password) && dr.GetString(2).Equals("admin"))
                {

                    id = dr.GetInt32(0);
                    dr.Close();
                    return 1;
                }
                else if (dr.GetInt32(0).Equals(login.Id) && dr.GetString(1).Equals(login.Password) && dr.GetString(2).Equals("client"))
                {
                    id = dr.GetInt32(0);
                    dr.Close();
                    return 2;
                }
            }
            return 3;
        }
        public bool ChangePassword(int id, string opwd , string npwd)
        {
            cmd.CommandText = "Update login SET password = '" + npwd + "'WHERE id ='" + id + "' AND password = '"+opwd+"'" ;
            int rows_affected = cmd.ExecuteNonQuery();
            if (rows_affected > 0)
            {
                return true;
            }
            else
                return false;
        
        }
    }
}
